<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_adminuser extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		/* require('engine/class.www-database.php');
		$this->conn=new WWW_Database(); */				
		require_once('engine/include_dao.php');
		return true;
		
	}
	public function login($data)
	{	//print_r($data);exit;
		$adminusers = new AdminUser();
		$adminusers->user_id = $data['user_id'];     
		$adminusers->password = md5($data['password']);
		$result = DAOFactory::getAdminUsersDAO()->login($adminusers);
		//print_r($result);exit;
		if($result)
		{
			return array('message'=>'login successfully', 'result'=>$result);
		}else
		{
			return array('message'=>'login failed');
		}
	}	
	public function create_adminuser($data)
	{
		$adminusers = new AdminUser();
		$adminusers->user_id = $data['user_id'];     
		$adminusers->middle_name = $data['middle_name'];
		$adminusers->last_name = $data['last_name'];
		$adminusers->email = $data['email'];
		$adminusers->first_name = $data['first_name'];
		$adminusers->password = md5($data['password']);
		$adminusers->role_id = $data['role_id'];
		$adminusers->status = $data['status'];
		$adminusers->updated_date = date("Y-m-d h:i:s");
		$adminusers->createdDate =$data['created_date'];
		$result = DAOFactory::getAdminUsersDAO()->insert($adminusers);
		//print_r($result);exit;
	/* $query="insert into `admin_users` (first_name,middle_name,last_name,email,user_id,password,role_id,created_date,updated_date) VALUES ('".$data['first_name']."','".$data['middle_name']."','".$data['last_name']."','".$data['email']."','".$data['user_id']."','".md5($data['password'])."','".$data['role_id']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);	 */
		//$this->db->last_query();exit;
	if($result !==""){
			return array('message'=>'Admin User successfully created');			
		}else{
			return array('message'=>'Admin User Creation Failed');
		}	
	}
	public function edit_adminuser($data)
	{
		$adminusers = new AdminUser();
		$adminusers->user_id = $data['user_id'];     
		$adminusers->admin_id = $data['admin_id'];     
		$adminusers->middle_name = $data['middle_name'];
		$adminusers->last_name = $data['last_name'];
		$adminusers->email = $data['email'];
		$adminusers->first_name = $data['first_name'];
		$adminusers->password = md5($data['password']);
		$adminusers->role_id = $data['role_id'];
		$adminusers->status = $data['status'];
		$adminusers->updated_date = date("Y-m-d h:i:s");
		$result = DAOFactory::getAdminUsersDAO()->update($adminusers);
		//print_r($result);exit;
	/* $query="update `admin_users` set first_name='".$data['first_name']."',middle_name='".$data['middle_name']."',last_name='".$data['last_name']."',email='".$data['email']."',user_id='".$data['user_id']."',password='".$data['password']."',role_id='".$data['role_id']."',updated_date='".date("Y-m-d H:i:s")."' where admin_id=".$data['admin_id'];
		$result=$this->conn->dbCommand($query);	 */	
		if($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function get_adminuser($data)
	{	
			$adminusers = new AdminUser();
			$adminusers->admin_id = $data['admin_id'];
			$result = DAOFactory::getAdminUsersDAO()->load($adminusers);
			//print_r($result);exit;
		
		/*  $query="SELECT admin_users.first_name,middle_name,last_name,email,user_id,password ,status,roles.role_id,roles.first_name AS role_name FROM admin_users
LEFT JOIN roles ON roles.role_id=admin_users.role_id where admin_id='".$data['admin_id']."'";
			
		$result=$this->conn->dbMultiple($query); */		
		if($result !== ""){
			return array('message'=>'Role get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Role get Failed');
		}	
		
	}
	public function get_adminuser_roles()
	{	
		$result = DAOFactory::getAdminUsersDAO()->load_roles();
		//print_r($result);exit;
		//$query = "select first_name,role_id from roles";
		//$result = $this->conn->dbMultiple($query);
		if($result!="")
		{
			return array('message'=>'admin roles get successfully', 'result'=>$result);
		}else
		{
			return array('message'=>'admin roles get failed');
		}
	}	
	public function delete_adminuser($data){
			$adminusers = new AdminUser();
			$adminusers->admin_id = $data['admin_id'];
			$adminusers->status = $data['status'];
			$result = DAOFactory::getAdminUsersDAO()->delete_adminuser($adminusers);
			 if($result == 1)
			return array('message'=>"Admin User Deleted Successfully");		
		else
			return array('message'=>'Admin User Deletion Fails');
	}
	public function all_adminusers($data){
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string = "select * from `admin_users`";
	$query_string1 = "SELECT count(*) as total from admin_users";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		if($data['search_by']=='status')
		{
			$search_by = "status";
			
		}else if($data['search_by'] == 'first_name')
		{
			$search_by = "first_name";
			
		}else if($data['search_by'] == 'last_name')
		{
			$search_by = "last_name";
			
		}
		else if($data['search_by'] == 'email')
		{
			$search_by = "email";
			
		} 
			$adminusers = new AdminUser();
			$adminusers->offset = $offset;
			$adminusers->order_by = $order_by;
			$adminusers->sort_order = $sort_order;
			$adminusers->results_per_page = $results_per_page;
			$adminusers->search_by = $search_by;
			$adminusers->search_by_value = $data['search_by_value'];
			$result = DAOFactory::getAdminUsersDAO()->queryAllSearchBy($adminusers);
			$totalrows = DAOFactory::getAdminUsersDAO()->searchby_count($adminusers);
	}else
	{
			$adminusers = new AdminUser();
			$adminusers->offset = $offset;
			$adminusers->order_by = $order_by;
			$adminusers->sort_order = $sort_order;
			$adminusers->results_per_page = $results_per_page;
			$result = DAOFactory::getAdminUsersDAO()->queryAll($adminusers);
			$totalrows = DAOFactory::getAdminUsersDAO()->adminuser_count($adminusers);			
	}	
		if($result!==""){
			return array('message'=>"All Adminusers","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Admin User');
	
	}
}
	
?>